<?php // v.php view article
require_once('./inc/inc.common.php');
$tmpl 			= new mvTemplate();

// Load the article
$article = article::load($_REQUEST['a']);

if(!$article){
	trigger_error('article not found');
	header('location: index.php');
	die();
}

// check authorization
if( $article->approved != "Y" && !(isset($_SESSION['user']) && ( $_SESSION['user']->isAdmin || $_SESSION['user']->id == $article->authorID )) ){
	mvError::userError('Article Not Approved', 'This article has not been approved yet. You have been redirected.');
	header('location: index.php');
	die();
}

// Check Delete Requests
if(isset($_REQUEST['delreq']) && isset($_SESSION['user']) && ( $_SESSION['user']->isAdmin || $_SESSION['user']->id == $article->authorID ))
	mvError::userNotice('Are you Sure', 'Are you sure you want to delete this article? <a href="v.php?a='.$article->id.'&delcon=true">Click Here to Confirm</a>');

if(isset($_REQUEST['delcon']) && isset($_SESSION['user']) && ( $_SESSION['user']->isAdmin || $_SESSION['user']->id == $article->authorID )){
	$article->delete();
	mvError::userNotice('Article Deleted', 'The Article has been deleted.');
	header('location: index.php');
	die();
}

if(isset($_REQUEST['delattachreq']) && isset($_SESSION['user']) && ( $_SESSION['user']->isAdmin || $_SESSION['user']->id == $article->authorID ))
	mvError::userNotice('Are you Sure', 'Are you sure you want to delete this attachment? <a href="v.php?a='.$article->id.'&delattachcon='.$_REQUEST['delattachreq'].'">Click Here to Confirm</a>');

if(isset($_REQUEST['delattachcon']) && isset($_SESSION['user']) && ( $_SESSION['user']->isAdmin || $_SESSION['user']->id == $article->authorID )){
	$article->del_file($_REQUEST['delattachcon']);
	mvError::userNotice('Article Deleted', 'The Article has been deleted.');
	header('location: v.php?a=' . $article->id);
	die();
}

// all is well display the article
if($parent = $article->parent())
	$tmpl->assign('parent', $parent);

if($author = $article->author())
	$tmpl->assign('author', $author);

$tmpl->assign('article', $article);
$tmpl->assign('attach', $article->attachments());
$tmpl->assign('related', $article->related());
$tmpl->assign('comment', $article->comments());

$tmpl->page_title($article->title);

// Update the views
$article->incrementViews();

// Update Rating
// TODO: Keep people from rating twice (Cookies?)
if(isset($_POST['score']) && $_POST['score'] >= 0 && $_POST['score'] <=5 ){
	$article->addScore($_POST['score']);
}

// Add Comment
if(isset($_POST['commenttitle'])){
	if(strlen($_POST['commenttitle']) < 5 || strlen($_POST['commenttext']) < 5){
		mvError::userError('Comment not added', 'Your comment could not be added.  You need both a a title and text. Click <a href="#comment">Here</a> to edit your comment.');
	}else{
		$uid = isset($_SESSION['user']) ? $_SESSION['user']->id : false;
		$comment = $article->addComment($_POST['commenttitle'], $_POST['commenttext'], $uid);

		mvError::userNotice('Comment Added', 'Your comment has been submitted for approval.  Here is a <a href="v.php?a=' . $article->id . '#vc' . $comment->id.'">permalink</a> to your comment for later.');
		header('location: v.php?a=' . $article->id . '#vc' . $comment->id);
		die();
	}
}

$tmpl->page_display('v.tpl');
?>